package cn.xlmdz.subsoilerinfoquery.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import cn.xlmdz.subsoilerinfoquery.bean.LoginBean;
import cn.xlmdz.subsoilerinfoquery.databinding.ActivityLoginBinding;
import cn.xlmdz.subsoilerinfoquery.http.ApiEngine;
import cn.xlmdz.subsoilerinfoquery.http.ApiServices;
import cn.xlmdz.subsoilerinfoquery.utils.SharedPrefsUtil;
import cn.xlmdz.subsoilerinfoquery.utils.ToastUtil;
import cn.xlmdz.subsoilerinfoquery.view.LoadingDialog;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding viewBinding;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());

        initViews();
    }

    private void initViews() {
        String username = SharedPrefsUtil.getStringPreference(LoginActivity.this, "username");
        String password = SharedPrefsUtil.getStringPreference(LoginActivity.this, "password");
        viewBinding.etLoginUsername.setText(username);
        viewBinding.etLoginPassword.setText(password);
        viewBinding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = viewBinding.etLoginUsername.getText().toString().trim();
                String password = viewBinding.etLoginPassword.getText().toString().trim();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    ToastUtil.showTextToast("请填写用户名和密码");
                    return;
                }
                login(username, password);
            }
        });
    }

    private void login(String username, String password) {
        showLoading();
        ApiServices services = ApiEngine.getInstance().getApiService();
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        Observable<LoginBean> observable = services.login(params);
        observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull LoginBean loginBean) {
                        if (null == loginBean) {
                            ToastUtil.showTextToast("请求出错");
                            return;
                        }
                        if (loginBean.getCode() != 200) {
                            ToastUtil.showTextToast(loginBean.getMsg());
                            return;
                        }
                        if (TextUtils.isEmpty(loginBean.getData().getToken())) {
                            ToastUtil.showTextToast("登录错误");
                            return;
                        } else {
                            boolean isSuss = SharedPrefsUtil.setStringPreference(LoginActivity.this, "loginToken", loginBean.getData().getToken());
                            SharedPrefsUtil.setStringPreference(LoginActivity.this, "username", username);
                            SharedPrefsUtil.setStringPreference(LoginActivity.this, "password", password);
                            Intent intent = new Intent(LoginActivity.this, QueryOrganizeActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        hideLoading();
                        ToastUtil.showTextToast("登录失败");
                    }

                    @Override
                    public void onComplete() {
                        hideLoading();
                    }
                });
    }

    public void showLoading() {
        if (loadingDialog == null)
            loadingDialog = new LoadingDialog(this, "正在登录…");
        if (!loadingDialog.isShowingNew())
            loadingDialog.show();
    }

    public void hideLoading() {
        if (loadingDialog != null && loadingDialog.isShowingNew())
            loadingDialog.close();
    }
}